/*
 * This file is part of Sparwe.
 *
 *  Sparwe is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Sparwe is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Sparwe.  If not, see <http://www.gnu.org/licenses/>.
 */ 
#include "device.h"
#include "driver.h"
#include "configreader.h"
#include "eventmanager.h"
#include "policies/conversionpolicies.h"

using namespace sp_core;

Device::Device(DeviceSettings* settings)
{
	mpSettings = settings;
	mWindow.Create(sf::VideoMode(800,600,32), "Foo");
	mEvents = new EventManager(&mWindow);
	mEvents->Start();
}

Device::~Device()
{
	mEvents->Stop();
	delete mEvents;
}

void Device::Display()
{
	mWindow.Display();
}

void Device::Update()
{
	mWindow.Display();


	sf::Event event;
	while (mWindow.GetEvent(event))
	{
		mEvents->PushEvent(&event);
	}	
}

char Device::GetVersion() const 
{

	return 'A';
}

void Device::EnableMode(GLenum cap) const
{

}

void Device::DisableMode(GLenum cap) const
{


}


bool Device::SetFullscreen(bool mode)
{
	return true;

}

std::vector<std::string> Device::GetCapabilities() const
{
	return std::vector<std::string>();
}


Driver* Device::CreateDriver()
{

	return new Driver();
}


Device* Device::CreateDevice(std::string filename)
{
	DeviceSettings* settings = new DeviceSettings();
	
	ConfigReader reader(filename);

	int width = reader.GetOption<int, sp_policies::StringToIntPolicy>("width");
	int height = reader.GetOption<int, sp_policies::StringToIntPolicy>("height");
	int bitsPerPixel = reader.GetOption<int, sp_policies::StringToIntPolicy>("bpp");

	settings->mWindow.SetWidth(width);
	settings->mWindow.SetHeight(height);
	settings->mWindow.SetBitsPerPixel(bitsPerPixel);

	int xpos = reader.GetOption<int, sp_policies::StringToIntPolicy>("xpos");
	int ypos = reader.GetOption<int, sp_policies::StringToIntPolicy>("ypos");
	bool fullscreen = reader.GetOption<bool, sp_policies::StringToBoolPolicy>("fullscreen");

	settings->mWindow.SetXPosition(xpos);
	settings->mWindow.SetYPosition(ypos);
	settings->mWindow.SetFullscreen(fullscreen);


	Device* device = new Device(settings);
	return device;
}

void Device::CreateEventManager()
{

}
